package com.zmovie.admin.controller.hall;

import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.HallType;
import com.zmovie.framework.service.HallTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 影厅类型管理模块
 * @since 2023-12-02 17:04
 */
@Controller
@RequestMapping("/hall/type")
@Api(tags = "影厅类型管理模块")
public class HallTypeController {
    @Autowired
    private HallTypeService hallTypeService;
    @GetMapping
    @ApiOperation("影厅类型管理页面")
    public String index(Model model){
        //查类型
        List<HallType> hallTypes = hallTypeService.listHallType(new HallType());
        model.addAttribute(Constants.HALL_TYPES_KEY, hallTypes);
        return "hall/hall_type";
    }
    @GetMapping("/{id}")
    @ApiOperation("根据获取影厅类型")
    @ResponseBody
    public Object getHallType(@PathVariable("id") Integer id){
        return CommonReturnType.success(hallTypeService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加影厅类型")
    @ResponseBody
    public Object addHallType(@Validated HallType hallType){
        hallTypeService.save(hallType);
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改影厅类型")
    @ResponseBody
    public Object updateHallType(@Validated HallType hallType) throws BusinessException {
        if(hallType.getId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        hallTypeService.updateById(hallType);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除影厅类型")
    @ResponseBody
    public Object deleteHallType(@PathVariable("id") Integer id){
        hallTypeService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除影厅类型")
    @ResponseBody
    public Object deleteHallTypeBatch(Integer[] id){
        hallTypeService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索")
    public String search(HallType hallType, Model model){
        List<HallType> hallTypes = hallTypeService.listHallType(hallType);
        model.addAttribute(Constants.HALL_TYPES_KEY, hallTypes);
        return "hall/hall_type :: typeList";
    }
}
